/**
* @Title: warningMedical_ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @author EX-XIEYUWEN001
* @date 2014年11月6日 下午4:43:38
* @version V1.0
*/
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('WarningStaffCtrl', ['$scope','$http','$modal','BaseURL','Modal','WarningAddService',
	                                function($scope,$http,$modal,baseUrl,Modal,WarningAddService){
		$scope.serv = WarningAddService;
		
		$scope.$watch("tableData",function(v){
			$scope.serv.warningStaff=v;
		});
		
		if(WarningAddService.warningStaff){
			$scope.tableData=WarningAddService.warningStaff;
		}else{
			$scope.tableData=[];
		}
		$scope.createMedical = function() {
			var modalInstance = $modal.open({
			      templateUrl: baseUrl+'views/hcpms/warning/warnEdit/warningAddStaffDialog_view.html',
			      backdrop:'static',
				  size:'sm',
				  controller: 'WarningAddStaffCtrl'
			    });
			 //接收返回数据
			 modalInstance.result.then(function(item) {
				 $scope.tableData = item;
	 	    });
		}
		$scope.deleteMedical = function() {
			if(!$scope.tableData){
				return;
			}
			angular.forEach($scope.tableData,function(data,index){
			   if(data.checked){
				   $scope.tableData.splice(index,1);
			   }
		   });
		}
		//全选或者不全选
		$scope.checked = false;
		$scope.onRowCheckChange = function (checked){
			angular.forEach($scope.tableData,function(data,index){
				data.checked = checked;
			});
		}
	}]).
	controller('WarnAddStaffCtrl',
			['$scope','BaseURL','$modalInstance', 'Modal','WarningAddService', function($scope, baseUrl,$modalInstance, Modal,WarningAddService){
				$scope.serv = WarningAddService;
				$scope.form = {};
				$scope.warningMedicals = [];
				$scope.onWarningMedical = function (){
					if(!($scope.form.name&&!angular.isUndefined($scope.form.name))){
						Modal.alert("姓名不能为空！");
						return null;
					}
					WarningAddService.getWarningStaffList({name:$scope.form.name}).
					success(function(res){
						$scope.tableDatas = res.warningMedical;
					});
				}
				 //保存
		    	  $scope.confirm = function (){
		    		  var num = 0;
		    		  var isFlat = true;
		    		  angular.forEach($scope.tableDatas,function(data,index){
						   if(data.checked){
							   if(WarningAddService.warningStaff.length > 0){
								   angular.forEach(WarningAddService.warningStaff,function(datas,indexs){
									   if(datas.id == data.id){
										   isFlat = false;
									   }
								   });
								   if(isFlat){
									   $scope.serv.warningStaff.push(data); 
								   }
							   }else{
								   $scope.serv.warningStaff.push(data); 
							   }
							   data.checked = false;
							   num++;
						   }
					   });
		    		  if(num == 0){
		    			  Modal.alert("尚未勾选信息！");
							return null;
		    		  }
				     $modalInstance.close(WarningAddService.warningStaff);
		    	  }  
				$scope.onClose = function(){
    	    		$modalInstance.dismiss();
    	    	}
				//重置
				$scope.onReset = function (){
					$scope.form={};
				}
	}]).
	controller('addStaffCtrl',
			['$scope','BaseURL','$modalInstance', 'Modal','WarningAddService', function($scope, baseUrl,$modalInstance, Modal,WarningAddService){
				$scope.serv = WarningAddService;
				$scope.form = {};
				$scope.warningMedicals = [];
				$scope.onWarningMedical = function (){
					if(!($scope.form.name&&!angular.isUndefined($scope.form.name))){
						Modal.alert("姓名不能为空！");
						return null;
					}
					WarningAddService.getWarningStaffList({name:$scope.form.name}).
					success(function(res){
						$scope.tableDatas = res.warningMedical;
					});
				}
				 //保存
		    	  $scope.confirm = function (){
		    		  var num = 0;
		    		  var isFlat = true;
		    		  var resultArray = [];
		    		  angular.forEach($scope.tableDatas,function(data,index){
						   if(data.checked){
							   resultArray.push(data); 
							   data.checked = false;
							   num++;
						   }
					   });
		    		  if(num == 0){
		    			  Modal.alert("尚未勾选信息！");
							return null;
		    		  }
	    			  $modalInstance.close(resultArray);
		    	  }  
				$scope.onClose = function(){
    	    		$modalInstance.dismiss();
    	    	}
				//重置
				$scope.onReset = function (){
					$scope.form={};
				}
	}]);
})();